class LoginController < ApplicationController
  def index
    #    skkd
  end
  def login
    if request.get?
      session[:student_id] = nil
      session[:student_name] = nil
      session[:student_type] = nil
      session[:teacher_id] = nil
      session[:teacher_name] = nil
    end

    if request.post?
      name = params[:name]
      password = params[:password]
      user_type = params[:user_type]
      if "stu"==user_type[0]
        #        s = Student.student_login(name, password)
        s = Student.find(:first, :conditions =>
            ["sname = ? AND spassword = ? ", name, password] )
        if s
          session[:student_id] = s.id
          session[:student_name] = s.sname
          session[:student_type] = s.stype
          redirect_to(:controller => "stu", :action => "index" )
        else
          flash[:notice] = "用户名或密码错误！"
          redirect_to(:action => "index")
        end
      end

      if user_type[0] == "tea"
        #       t = Teacher.teacher_login(name, password)
        t = Teacher.find(:first, :conditions => ["tname = ? AND tpassword = ?", name, password])
        if t
          session[:teacher_id] = t.id
          session[:teacher_name] = t.tname
          redirect_to(:controller => "tea", :action => "index")
        else
          flash[:notice] = "用户名或密码错误！"
          redirect_to(:action => "index")
        end
      end
    end
  end

  def logout
    session[:student_id] = nil
    session[:student_name] = nil
    session[:student_type] = nil
    session[:teacher_id] = nil
    session[:teacher_name] = nil
    flash[:notice] = "已经退出。"
    redirect_to(:action => "index")
  end
end
